package main;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ConnectionDialog extends JDialog {
	
	private static final String DEFAULT_CONNECT_CONFIG_PATH = "configs/connect.config";
	private static final String IP_KEY = "ip";
	private static final String DB_KEY = "database";
	private static final String UN_KEY = "username";
	private static final String PW_KEY = "password";
	private static final String BLANK = "";

	private final JPanel contentPanel = new JPanel();
	private JTextField _ipField;
	private JTextField _usernameField;
	private JPasswordField _passwordField;
	private JTextField _databaseField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ConnectionDialog dialog = new ConnectionDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void launchDialog() {
		try {
			ConnectionDialog dialog = new ConnectionDialog();
			dialog.loadConnectConfig();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void loadConnectConfig() {
		final File connectConfig = new File(DEFAULT_CONNECT_CONFIG_PATH);
		
		if(connectConfig != null && connectConfig.exists()) {
			try {
				final Properties connectSettings = new Properties();
				connectSettings.load(new FileInputStream(connectConfig));
				_ipField.setText(connectSettings.getProperty(IP_KEY, BLANK));
				_usernameField.setText(connectSettings.getProperty(UN_KEY, BLANK));
				_passwordField.setText(connectSettings.getProperty(PW_KEY, BLANK));
				_databaseField.setText(connectSettings.getProperty(DB_KEY, BLANK));
			} 
			catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Create the dialog.
	 */
	public ConnectionDialog() {
		setTitle("JFinance");
		setBounds(100, 100, 400, 250);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.NORTH);
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			{
				JLabel titleLabel = new JLabel("Connect to the Database");
				titleLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
				panel.add(titleLabel);
			}
		}
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][][grow][]", "[30.00][30.00][30.00][30.00]"));
		{
			JLabel ipLabel = new JLabel("IP Address:");
			ipLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
			contentPanel.add(ipLabel, "cell 1 0,alignx trailing");
		}
		{
			_ipField = new JTextField();
			contentPanel.add(_ipField, "cell 2 0,growx");
			_ipField.setColumns(10);
		}
		{
			JLabel usernameLabel = new JLabel("Username:");
			usernameLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
			contentPanel.add(usernameLabel, "cell 1 1,alignx trailing");
		}
		{
			_usernameField = new JTextField();
			contentPanel.add(_usernameField, "cell 2 1,growx");
			_usernameField.setColumns(10);
		}
		{
			JLabel passwordLabel = new JLabel("Password:");
			passwordLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
			contentPanel.add(passwordLabel, "cell 1 2,alignx trailing");
		}
		{
			_passwordField = new JPasswordField();
			contentPanel.add(_passwordField, "cell 2 2,growx");
		}
		{
			JLabel databaseLabel = new JLabel("Database:");
			databaseLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
			contentPanel.add(databaseLabel, "cell 1 3,alignx trailing");
		}
		{
			_databaseField = new JTextField();
			contentPanel.add(_databaseField, "cell 2 3,growx");
			_databaseField.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
